{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1,  2],\n       [ 1,  4],\n       [ 1,  0],\n       [10,  2],\n       [10,  4],\n       [10,  0]])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.array([[1, 2], [1, 4], [1, 0],\n",
    "              [10, 2], [10, 4], [10, 0]])\n",
    "X"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "kmeans = KMeans(n_clusters=2, random_state=0, n_init=\"auto\").fit(X)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 1, 1, 0, 0, 0])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmeans.labels_\n",
    "# kmeans.n_clusters"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 0])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array( [[0, 0], [12, 3]])\n",
    "result = kmeans.predict(x)\n",
    "result"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "0.7133477791749615"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型评估\n",
    "# KMEANS-评估有问题\n",
    "from sklearn.metrics import silhouette_score\n",
    "# silhouette_score(X,kmeans.labels_)\n",
    "# silhouette_score(X,kmeans.predict(X))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-7.72642091, -8.39495682],\n       [ 5.45339605,  0.74230537],\n       [-2.97867201,  9.55684617],\n       [ 6.04267315,  0.57131862],\n       [-6.52183983, -6.31932507],\n       [ 3.64934251,  1.40687195],\n       [-2.17793419,  9.98983126],\n       [ 4.42020695,  2.33028226],\n       [ 4.73695639,  2.94181467],\n       [-3.6601912 ,  9.38998415],\n       [-3.05358035,  9.12520872],\n       [-6.65216726, -5.57296684],\n       [-6.35768563, -6.58312492],\n       [-3.6155326 ,  7.8180795 ],\n       [-1.77073104,  9.18565441],\n       [-7.95051969, -6.39763718],\n       [-6.60293639, -6.05292634],\n       [-2.58120774, 10.01781903],\n       [-7.76348463, -6.72638449],\n       [-6.40638957, -6.95293851],\n       [-2.97261532,  8.54855637],\n       [-6.9567289 , -6.53895762],\n       [-7.32614214, -6.0237108 ],\n       [-2.14780202, 10.55232269],\n       [-2.54502366, 10.57892978],\n       [-2.96983639, 10.07140835],\n       [ 3.22450809,  1.55252436],\n       [-6.25395984, -7.73726715],\n       [-7.85430886, -6.09302499],\n       [-8.1165779 , -8.20056621],\n       [-7.55965191, -6.6478559 ],\n       [ 4.93599911,  2.23422496],\n       [ 4.44751787,  2.27471703],\n       [-5.72103161, -7.70079191],\n       [-0.92998481,  9.78172086],\n       [-3.10983631,  8.72259238],\n       [-2.44166942,  7.58953794],\n       [-2.18511365,  8.62920385],\n       [ 5.55528095,  2.30192079],\n       [ 4.73163961, -0.01439923],\n       [-8.25729656, -7.81793463],\n       [-2.98837186,  8.82862715],\n       [ 4.60516707,  0.80449165],\n       [-3.83738367,  9.21114736],\n       [-2.62484591,  8.71318243],\n       [ 3.57757512,  2.44676211],\n       [-8.48711043, -6.69547573],\n       [-6.70644627, -6.49479221],\n       [-6.8666253 , -5.42657552],\n       [ 3.83138523,  1.47141264],\n       [ 2.02013373,  2.79507219],\n       [ 4.64499229,  1.73858255],\n       [-1.6966718 , 10.37052616],\n       [-6.6197444 , -6.09828672],\n       [-6.05756703, -4.98331661],\n       [-7.10308998, -6.1661091 ],\n       [-3.52202874,  9.32853346],\n       [-2.26723535,  7.10100588],\n       [ 6.11777288,  1.45489947],\n       [-4.23411546,  8.4519986 ],\n       [-6.58655472, -7.59446101],\n       [ 3.93782574,  1.64550754],\n       [-7.12501531, -7.63384576],\n       [ 2.72110762,  1.94665581],\n       [-7.14428402, -4.15994043],\n       [-6.66553345, -8.12584837],\n       [ 4.70010905,  4.4364118 ],\n       [-7.76914162, -7.69591988],\n       [ 4.11011863,  2.48643712],\n       [ 4.89742923,  1.89872377],\n       [ 4.29716432,  1.17089241],\n       [-6.62913434, -6.53366138],\n       [-8.07093069, -6.22355598],\n       [-2.16557933,  7.25124597],\n       [ 4.7395302 ,  1.46969403],\n       [-5.91625106, -6.46732867],\n       [ 5.43091078,  1.06378223],\n       [-6.82141847, -8.02307989],\n       [ 6.52606474,  2.1477475 ],\n       [ 3.08921541,  2.04173266],\n       [-2.1475616 ,  8.36916637],\n       [ 3.85662554,  1.65110817],\n       [-1.68665271,  7.79344248],\n       [-5.01385268, -6.40627667],\n       [-2.52269485,  7.9565752 ],\n       [-2.30033403,  7.054616  ],\n       [-1.04354885,  8.78850983],\n       [ 3.7204546 ,  3.52310409],\n       [-3.98771961,  8.29444192],\n       [ 4.24777068,  0.50965474],\n       [ 4.7269259 ,  1.67416233],\n       [ 5.78270165,  2.72510272],\n       [-3.4172217 ,  7.60198243],\n       [ 5.22673593,  4.16362531],\n       [-3.11090424, 10.86656431],\n       [-3.18611962,  9.62596242],\n       [-1.4781981 ,  9.94556625],\n       [ 4.47859312,  2.37722054],\n       [-5.79657595, -5.82630754],\n       [-3.34841515,  8.70507375]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}